{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_detectron_ops.dylib, 9): Library not loaded: @rpath/libtorch.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_detectron_ops.dylib\n",
      "  Reason: image not found\n",
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_module_test_dynamic.dylib, 9): Library not loaded: @rpath/libtorch.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_module_test_dynamic.dylib\n",
      "  Reason: image not found\n",
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_observers.dylib, 9): Library not loaded: @rpath/libtorch.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_observers.dylib\n",
      "  Reason: image not found\n",
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_global_deps.dylib, 9): Library not loaded: @rpath/libiomp5.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_global_deps.dylib\n",
      "  Reason: image not found\n",
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libshm.dylib, 9): Library not loaded: @rpath/libiomp5.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libshm.dylib\n",
      "  Reason: image not found\n",
      "cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_python.dylib, 9): Library not loaded: @rpath/libshm.dylib\n",
      "  Referenced from: /Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_python.dylib\n",
      "  Reason: image not found\n"
     ]
    }
   ],
   "source": [
    "// Load all LibTorch dependencies for cling\n",
    "#pragma cling add_library_path(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib\")\n",
    "#pragma cling add_include_path(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/include/\")\n",
    "#pragma cling add_include_path(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/include/torch/csrc/api/include/\")\n",
    "\n",
    "// Note: This is loaded for macOS environment, for Linux try loading *.so files\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libc10.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libiomp5.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_detectron_ops.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_module_test_dynamic.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libcaffe2_observers.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libfbjni.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libpytorch_jni.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_cpu.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_global_deps.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libshm.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch_python.dylib\")\n",
    "#pragma cling load(\"/Users/prabhuomkar/Projects/PyTorch/flare/lib/libtorch/lib/libtorch.dylib\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <torch/torch.h>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace at::indexing;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 7 8 9\n"
     ]
    }
   ],
   "source": [
    "std::vector<float> data_vector{1, 2, 3, 4, 5, 6, 7, 8, 9};\n",
    "std::cout << data_vector << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1\n",
      " 2\n",
      " 3\n",
      " 4\n",
      " 5\n",
      " 6\n",
      " 7\n",
      " 8\n",
      " 9\n",
      "[ CPUFloatType{9} ]\n"
     ]
    }
   ],
   "source": [
    "at::Tensor tensor_1d = torch::from_blob(data_vector.data(), {9});\n",
    "std::cout << tensor_1d << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1  2  3\n",
      " 4  5  6\n",
      " 7  8  9\n",
      "[ CPUFloatType{3,3} ]\n"
     ]
    }
   ],
   "source": [
    "at::Tensor tensor_2d = torch::from_blob(data_vector.data(), {3, 3});\n",
    "std::cout << tensor_2d << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1,.,.) = \n",
      "  1  2  3\n",
      "  4  5  6\n",
      "  7  8  9\n",
      "\n",
      "(2,.,.) = \n",
      "  10  11  12\n",
      "  13  14  15\n",
      "  16  17  18\n",
      "\n",
      "(3,.,.) = \n",
      "  19  20  21\n",
      "  22  23  24\n",
      "  25  26  17\n",
      "[ CPULongType{3,3,3} ]\n"
     ]
    }
   ],
   "source": [
    "at::Tensor tensor_3d = torch::tensor({\n",
    "    {\n",
    "        {1, 2, 3}, \n",
    "        {4, 5, 6}, \n",
    "        {7, 8, 9}\n",
    "    },\n",
    "    {\n",
    "        {10, 11, 12}, \n",
    "        {13, 14, 15}, \n",
    "        {16, 17, 18}\n",
    "    },\n",
    "    {\n",
    "        {19, 20, 21}, \n",
    "        {22, 23, 24}, \n",
    "        {25, 26, 17}\n",
    "    }\n",
    "});\n",
    "std::cout << tensor_3d << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Indexing in 1-D Tensor:\n",
      "-----------------------\n",
      "4\n",
      "[ CPUFloatType{} ]\n"
     ]
    }
   ],
   "source": [
    "// ---------- INDEXING ---------- //\n",
    "// 1-D Tensor\n",
    "std::cout << \"Indexing in 1-D Tensor:\" << std::endl;\n",
    "std::cout << \"-----------------------\" << std::endl;\n",
    "std::cout << tensor_1d[3] << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Indexing in 2-D Tensor:\n",
      "-----------------------\n",
      "5\n",
      "[ CPUFloatType{} ]\n"
     ]
    }
   ],
   "source": [
    "// 2-D Tensor\n",
    "std::cout << \"Indexing in 2-D Tensor:\" << std::endl;\n",
    "std::cout << \"-----------------------\" << std::endl;\n",
    "std::cout << tensor_2d[1][1] << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Indexing in 3-D Tensor:\n",
      "-----------------------\n",
      "22\n",
      "[ CPULongType{} ]\n"
     ]
    }
   ],
   "source": [
    "// 3-D Tensor\n",
    "std::cout << \"Indexing in 3-D Tensor:\" << std::endl;\n",
    "std::cout << \"-----------------------\" << std::endl;\n",
    "std::cout << tensor_3d[2][1][0] << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1\n",
      " 2\n",
      " 3\n",
      " 4\n",
      " 5\n",
      " 6\n",
      " 7\n",
      " 8\n",
      " 9\n",
      "[ CPUFloatType{9} ]\n",
      " 3\n",
      " 4\n",
      " 5\n",
      " 6\n",
      " 7\n",
      " 8\n",
      " 9\n",
      "[ CPUFloatType{7} ]\n",
      " 1\n",
      " 2\n",
      "[ CPUFloatType{2} ]\n",
      " 3\n",
      " 4\n",
      " 5\n",
      "[ CPUFloatType{3} ]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "@0x113079ec0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// ---------- SLICING ---------- //\n",
    "// 1-D Tensor\n",
    "std::cout << tensor_1d.index({Slice()}) << std::endl; // tensor_1d[:]\n",
    "std::cout << tensor_1d.index({Slice(2, None)}) << std::endl; // tensor_1d[2:]\n",
    "std::cout << tensor_1d.index({Slice(None, 2)}) << std::endl; // tensor_1d[:2]\n",
    "std::cout << tensor_1d.index({Slice(2, 5)}) << std::endl; // tensor_1d[2:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1  2  3\n",
      " 4  5  6\n",
      " 7  8  9\n",
      "[ CPUFloatType{3,3} ]\n",
      " 7\n",
      " 8\n",
      " 9\n",
      "[ CPUFloatType{3} ]\n",
      " 2\n",
      " 5\n",
      " 8\n",
      "[ CPUFloatType{3} ]\n"
     ]
    }
   ],
   "source": [
    "// 2-D Tensor\n",
    "std::cout << tensor_2d.index({Slice()}) << std::endl;\n",
    "std::cout << tensor_2d.index({2}) << std::endl;\n",
    "std::cout << tensor_2d.index({Slice(), 1}) << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 16\n",
      " 17\n",
      " 18\n",
      "[ CPULongType{3} ]\n",
      " 2\n",
      " 5\n",
      " 8\n",
      "[ CPULongType{3} ]\n",
      "  6\n",
      " 15\n",
      " 24\n",
      "[ CPULongType{3} ]\n",
      " 19  20  21\n",
      " 22  23  24\n",
      " 25  26  17\n",
      "[ CPULongType{3,3} ]\n",
      "  4   5   6\n",
      " 13  14  15\n",
      " 22  23  24\n",
      "[ CPULongType{3,3} ]\n",
      "  1   4   7\n",
      " 10  13  16\n",
      " 19  22  25\n",
      "[ CPULongType{3,3} ]\n"
     ]
    }
   ],
   "source": [
    "// 3-D Tensor\n",
    "std::cout << tensor_3d.index({1, 2}) << std::endl;\n",
    "std::cout << tensor_3d.index({0, Slice(), 1}) << std::endl;\n",
    "std::cout << tensor_3d.index({Slice(), 1, 2}) << std::endl;\n",
    "std::cout << tensor_3d.index({2}) << std::endl;\n",
    "std::cout << tensor_3d.index({Slice(), 1}) << std::endl;\n",
    "std::cout << tensor_3d.index({Slice(), Slice(), 0}) << std::endl;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
